
// 获取当前HOST
if (typeof http_url === 'undefined') {
    var http_urla = document.scripts;
    var http_urlb = http_urla[http_urla.length-1].src;
    var http_url = http_urlb.substring(0,http_urlb.lastIndexOf("/"));
    var http_url = http_url.substring(0,http_url.lastIndexOf("/")+1);
}
if (typeof http_link === 'undefined') {
    var http_link = '';
}
// 接口请求路径后缀
if(typeof http_fix === 'undefined') {
    var http_fix = '.html';
}

// 刷新jscss文件缓存
if(typeof cssjsv === 'undefined') {
    let min = 1;
    let max = 100;
    var cssjsv = Math.floor(Math.random() * (max - min + 1)) + min;
}
// 插件目录
var plugFileJedate = http_link+'plug/jedate/';   // 日期控件css路径
var plugFileJpicker = http_link+'plug/jpicker/'; // 颜色拾取器
var plugFileIframeUpload = http_link+'plug/iframeUpload/'; // 文件上传图标
var cutSubUrl = http_url+"c_upload_base64upload/go/1"+http_fix; // 图片裁剪上传


// 插入新的样式方法
function addNewStyle(newStyle) {
    var styleElement = document.getElementById('styles_js');
    if (!styleElement) {
        styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.id = 'styles_js';
        document.getElementsByTagName('head')[0].appendChild(styleElement);
    }
    styleElement.appendChild(document.createTextNode(newStyle));
}

//动态加载文件方法
function loadjscssfile(filename, filetype) {
    if (filetype == "js") {
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    } else if (filetype == "css") {
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref)
    }
}

// 加载组文件
function file_load(type){
    if(type == 'list'){
        list_file();
    }else if(type == 'info'){
        info_file();
    }
    afterLoading(type);
}

// 列表页加载
function list_file(){
    // layer
    loadjscssfile(http_link+'plug/layer3.1.1/layer.js?v='+cssjsv,'js');

    // 日期选择器
    loadjscssfile(http_link+'plug/jedate/jedate.min.js?v=' + cssjsv, 'js');

    // 图片预览
    loadjscssfile(http_link+'plug/viewer/viewer.min.css?v='+cssjsv,'css');
    loadjscssfile(http_link+'plug/viewer/viewer.min.js?v='+cssjsv,'js');
    addNewStyle('body .viewer-button {top:auto; left:auto; bottom:5px; right: 5px;width:40px;height:40px;}');
    addNewStyle('body .viewer-button:before {top:auto; left:auto; bottom: 10px; right: 10px; }');
}

// 详情页加载
function info_file(){
    // layer
    loadjscssfile(http_link+'plug/layer3.1.1/layer.js?v='+cssjsv,'js');

    // 日期选择器
    loadjscssfile(http_link+'plug/jedate/jedate.min.js?v=' + cssjsv, 'js');

    // 颜色选择器
    loadjscssfile(http_link+'plug/jpicker/css/jpicker-1.1.6.min.css?v='+cssjsv,'css');
    addNewStyle('body .jPicker .Icon {top: -5px;left:2px;}', 0);
    loadjscssfile(http_link+'plug/jpicker/jpicker-1.1.6.min.js?v='+cssjsv,'js');

    // 地区选择
    loadjscssfile(http_link+'plug/jquery.area/css/style.css?v='+cssjsv,'css');
    loadjscssfile(http_link+'plug/jquery.area/js/jquery.area.js?v='+cssjsv,'js');

    // 富文本
    loadjscssfile(http_link+'plug/wangEditor/v2.1.23/css/wangEditor.min.css?v='+cssjsv,'css');
    loadjscssfile(http_link+'plug/wangEditor/v2.1.23/js/wangEditor.min.js?v='+cssjsv,'js');

    // 文件上传
    loadjscssfile(http_link+'plug/iframeUpload/js/main.js?v='+cssjsv,'js');

    // 图片上传
    loadjscssfile(http_link+'plug/plugFileReaderBase64/css/style.css?v='+cssjsv,'css');
    loadjscssfile(http_link+'plug/plugFileReaderBase64/js/main.js?v='+cssjsv,'js');

    // 图片裁剪上传
    loadjscssfile(http_link+'plug/img_cut/img_cut.js?v='+cssjsv,'js');

    // 图片预览
    loadjscssfile(http_link+'plug/viewer/viewer.min.css?v='+cssjsv,'css');
    loadjscssfile(http_link+'plug/viewer/viewer.min.js?v='+cssjsv,'js');
    addNewStyle('body .viewer-button {top:auto; left:auto; bottom:5px; right: 5px;width:40px;height:40px;}');
    addNewStyle('body .viewer-button:before {top:auto; left:auto; bottom: 10px; right: 10px; }');
}

//载入后
function afterLoading(type){
    loadjscssfile(http_link+'admin/js/main.js?v='+cssjsv,'js');
}

//获取form 表单值
function getFormData(form) {
    var sformData = form.serializeArray();
    var formData = new FormData(); // 实例化创建form表单
    $.each(sformData, function () {
        formData.append(this.name, this.value); // 增加参数
    });
    return formData;
}

// layerOpen
function layerIframe(readUrl, w, h, p){
    layer.open({
        type: 2,
        title: false,
        shadeClose: true, //开启遮罩关闭
        shade: 0.8,
        area: [w, h], //宽高
        content: readUrl,
        cancel: function(index){
            layer.close(index);
            if(p == 1){
                window.location.reload();
            }
            if(p == 2){
                parent.location.reload();
            }
        }
    });
}

// 打开layer iframe
function layerIframeOpen(url, w, h, p){
    if(!url){
        layer.msg('非法访问');
    }else{
        layerIframe(url, w, h, p);
    }
}

// 下载文件
function downloadFile(url,name){
    const a = document.createElement("a");
    a.download = name;
    a.href = url;
    document.body.appendChild(a);
    a.click();
    a.remove();
}

// js_post提交
function jspost(url, params) {
    let temp = document.createElement("form");
    temp.action = url;
    temp.method = "post";
    temp.style.display = "none";
    for (let x in params) {
        /*if(Array.isArray(params[x])){}*/
        let opt = document.createElement("input");
        opt.name = x;
        opt.value = params[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}
/*
function postcall( url, params, target){
    var tempform = document.createElement("form");
    tempform.action = url;
    tempform.method = "post";
    tempform.style.display="none"
    if(target) {
        tempform.target = target;
    }
    for (var x in params) {
        var opt = document.createElement("input");
        opt.name = x;
        opt.value = params[x];
        tempform.appendChild(opt);
    }
    var opt = document.createElement("input");
    opt.type = "submit";
    tempform.appendChild(opt);
    document.body.appendChild(tempform);
    tempform.submit();
    document.body.removeChild(tempform);
}

function postCurrent(url,params){
    var form = $("<form method='post'></form>");
    var input;
    form.attr({"action":url});
    $.each(params,function (key,value) {
        input = $("<input type='hidden'>");
        input.attr({"name":key});
        input.val(value);
        form.append(input);
    });
    $(document.body).append(form);
    form.submit();
}

function postSend(url, params){
    let paramData = formatDate(params);
    var xhr=new XMLHttpRequest()
    // 这里要将格式化的数据作为参数传递给后台
    xhr.open('post', url, true );
    //使用post方式 需要设置一下header的content
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhr.send(paramData);
}

// 数组转换成可提交的数据串
function formatDate(postData) {
    let a = '';
    for(var i in postData){
        if(Array.isArray(postData[i])){
            for(var j=0;j<postData[i].length;j++){
                a += i + "%5B%5D=" + postData[i][j] + '&'
            }
        }else{
            a += i + "=" + postData[i] + '&'
        }
    }
    return a.substring(0, a.length-1);
}
*/

function get_radio_val(obj_name){
    var obj = document.getElementsByName(obj_name);
    for(i=0;i<obj_name.length;i++){
        if(obj[i].checked){
            return obj[i].value;
        }
    }
}